package com.myApp.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

import com.google.gwt.user.client.rpc.RemoteService;


/**
 * <p>Title: MyApp</p>
 * <p>Description: Main entry point class for module, </p>
 * <code>Entry point classes define <code>onModuleLoad()</code>.
 *
 * @author Louis Botterill
 * @version 1.0
 *
 * Date         Author  Description
 * 10-03-2008   LB      Initial template version
 */
public class MyApp implements EntryPoint
{
    /**
    * This is the entry point method.
    */
    public void onModuleLoad()
    {
        // simple example of some conponents (Button and label) and RPC
        //
        // 1. create a button and label
        // 2. button click calls simple RPC method, RPC callback return value updates the label with result from the call
        
        final Button button = new Button("Click me");
        final Label label = new Label();
        
        button.addClickListener(new ClickListener()
        {
            public void onClick(Widget sender)
            {
                if (label.getText().equals(""))
                {
                    label.setText("Hello World!");
                }
                else
                {
                    label.setText("");
                }
                
                // get our simple example RPC service
                MyServiceAsync myServiceAsync = getMyServiceAsync();
                
                AsyncCallback myServiceCallback = new AsyncCallback()
                {
                    public void onSuccess(Object result)
                    {
                        label.setText(label.getText() + " - " + (String)result);
                    }
                    public void onFailure(Throwable caught)
                    {
                        label.setText(label.getText() + " - server request raised an error; Java exception : " + caught == null ? "An unknown exception" : caught.getMessage());
                    }
                };
                
                try
                {
                    myServiceAsync.myMethod("some data", myServiceCallback);
                }
                catch (Exception e)
                {
                    label.setText("Server request raised an error: " + e.getMessage());
                }
            }
        });
        
        // use simple html placeholder elements
        RootPanel.get("slot1").add(button);
        RootPanel.get("slot2").add(label);
    }
    
    /**
     * simple static helper to return an instance of the client side RPC proxy mapped to the service servlet url mapping
     */
    public static MyServiceAsync getMyServiceAsync()
    {
        MyServiceAsync service = (MyServiceAsync)GWT.create(MyService.class);
        
        ServiceDefTarget endpoint = (ServiceDefTarget)service;
        
        // create a url based on moduleBaseURL / myservice - this URL should be mapped to the implementation servlet in WEB-INF/web.xml
        String moduleRelativeURL = GWT.getModuleBaseURL() + "myservice";
        
        endpoint.setServiceEntryPoint(moduleRelativeURL);
        
        return service;
    }
}

